/**
 * 
 */
package com.zaprit.common.controller;

import java.util.Date;
import java.util.List;

import com.rdo.common.bo.Attachment;
import com.rdo.util.context.Context;
import com.rdo.util.web.service.ServiceController;
import com.rdo.util.web.service.bo.ServiceRequest;
import com.rdo.util.web.service.bo.ServiceResponse;
import com.rdo.util.web.service.exception.ServiceParameterException;
import com.zaprit.common.exception.CommonException;
import com.zaprit.common.service.CommonService;

/**
 * @author Dell
 */
public class CommonController implements ServiceController
{
	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws CommonException
	 */
	public void addAttachment(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, CommonException
	{
		String scopeId = serviceRequest.getMandatoryString("scopeId", 40);
		String userId = serviceRequest.getMandatoryString("userId", 40);
		int type = serviceRequest.getInt("type", Attachment.TYPE_FILE);
		String encoding = serviceRequest.getString("encoding", 20);

		Attachment attachment = new Attachment();
		attachment.setCreatedBy(userId);
		attachment.setCreatedOn(new Date());
		attachment.setEncoding((encoding == null || encoding.isEmpty()) ? "UTF-8" : encoding);
		attachment.setType(type);
		attachment.setVisibility(serviceRequest.getMandatoryString("visibility", 20));
		attachment.setComments(serviceRequest.getMandatoryString("comments", 500));

		CommonService commonService = (CommonService) Context.getBean("commonService");
		if (type == Attachment.TYPE_FILE)
		{
			attachment.setName(serviceRequest.getMandatoryString("fileName", 255));
			attachment.setFileSize(serviceRequest.getMandatoryLong("fileSize"));
			commonService.addAttachment(scopeId, userId, attachment);
			attachment.setServerPath(scopeId + "/" + userId + "/" + attachment.getAttachmentId() + "/file");
			commonService.updateAttachment(scopeId, userId, attachment);
		}
		else
		{
			throw new CommonException("No other Type of attachment supported");
			// attachment.setName("Web Link");
			// attachment.setPath(serviceRequest.getMandatoryString("path",
			// 2000));
			// commonService.addAttachment(userId, attachment);
		}
		serviceResponse.setResult(attachment);
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws CommonException
	 */
	public void getAttachments(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, CommonException
	{
		String scopeId = serviceRequest.getMandatoryString("scopeId", 40);
		String userId = serviceRequest.getMandatoryString("userId", 40);
		List<String> attachmentIds = serviceRequest.getMandatoryStringList("attachmentIds", 40);

		CommonService commonService = (CommonService) Context.getBean("commonService");
		serviceResponse.setResult(commonService.getAttachments(scopeId, userId, attachmentIds));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws CommonException
	 */
	public void getAttachment(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, CommonException
	{
		String scopeId = serviceRequest.getMandatoryString("scopeId", 40);
		String userId = serviceRequest.getMandatoryString("userId", 40);
		String attachmentId = serviceRequest.getMandatoryString("attachmentId", 40);

		CommonService commonService = (CommonService) Context.getBean("commonService");
		serviceResponse.setResult(commonService.getAttachment(scopeId, userId, attachmentId));
	}
}
